package com.wzq.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.dao.pojo.Discuss;
import com.wzq.dao.pojo.PageBean;
import com.wzq.dao.pojo.R;
import com.wzq.dao.pojo.Wechat_user;
import com.wzq.service.IDiscussService;
import com.wzq.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/discuss")
public class AdminDiscussController extends GetSome{

    @Autowired
    IDiscussService iDiscussService;
    AdminUserController adminUserController;
    @RequestMapping("/list")        //添加
    public R list(@RequestBody PageBean pageBean) {
        String query=pageBean.getQuery().trim();
        Wechat_user wechat_user = new Wechat_user();
        Page<Discuss> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
        Page<Discuss> pageResult = iDiscussService.page(page, new QueryWrapper<Discuss>().in(StringUtil.isNotEmpty(query),"w_id", searchByWname(query)));
        Map<String,Object> map=new HashMap<>();
        //        添加用户名称和项目名称
        List<Discuss> discusses=new ArrayList<>();
        for (Discuss discuss : pageResult.getRecords()) {
            discuss.setW_name( getW_name(discuss.getW_id()) );
            discuss.setP_name( getP_name(discuss.getP_id()) );
            discusses.add(discuss);
        }
        map.put("DiscussList",pageResult.getRecords());
        map.put("total",pageResult.getTotal());
        return R.ok(map);
    }

    @RequestMapping("/save")        //添加修改
    public R save(@RequestBody Discuss discuss){
        if(discuss.getId()==null || discuss.getId()==-1){
            iDiscussService.save(discuss);
        }else{
            iDiscussService.saveOrUpdate(discuss);
        }
        return R.ok();
    }

    @GetMapping("/{id}")            //单个回显
    public R findById(@PathVariable(value = "id")Integer id){
        Discuss discuss = iDiscussService.getById(id);
        //        获得用户名称和项目名称
        discuss.setW_name( getW_name(discuss.getW_id()) );
        discuss.setP_name( getP_name(discuss.getP_id()) );
        Map<String,Object> map=new HashMap<>();
        map.put("discuss",discuss);
        return R.ok(map);
    }

    @GetMapping("/delete/{id}")     //删除
    public R delete(@PathVariable(value = "id")Integer id){
        iDiscussService.removeById(id);
        return R.ok();
    }
}
